﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;
using QuickUIT.Input;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIEditTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetDisabledState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox4'");
            Assert.AreEqual(false, textBox.IsEnabled);
        }

        [TestMethod]
        public void ShouldGetReadOnlyState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox4'");
            Assert.AreEqual(true, textBox.IsReadOnly);
        }

        [TestMethod]
        public void ShouldGetValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual("Text Box", textBox.Value);
        }

        [TestMethod]
        public void ShouldSetValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual("Text Box", textBox.Value);
            textBox.Value = "Foo Bar";
            Assert.AreEqual("Foo Bar", textBox.Value);
        }

        [TestMethod]
        public void ShouldTypeText()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox3'");
            Assert.AreEqual(String.Empty, textBox.Value);
            textBox.SetFocus();
            textBox.TypeText("ABCDEFG"); 
            Assert.AreEqual("ABCDEFG", textBox.Value);
        }
        
        [TestMethod]
        public void ShouldClearText()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual("Text Box", textBox.Value);
            textBox.Clear();
            Assert.AreEqual(String.Empty, textBox.Value);
        }

        [TestMethod]
        public void ShouldGetIsPassword()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit passwordBox = window.UIEdit("@AutomationId='passwordBox1'");
            Assert.IsTrue(passwordBox.IsPassword);

            UIEdit textBox = window.UIEdit("@AutomationId='textBox3'");
            Assert.IsFalse(textBox.IsPassword);
        }

        [TestMethod]
        public void GetDefaultMaximumValueForUnsupportedValueControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual(double.NaN, textBox.Maximum);
        }

        [TestMethod]
        public void GetDefaultMinimumValueForUnsupportedValueControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual(double.NaN, textBox.Minimum);
        }

        [TestMethod]
        public void GetDefaultSmallChangeForUnsupportedValueControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual(double.NaN, textBox.SmallChange);
        }

        [TestMethod]
        public void GetSupportedTextSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            Assert.AreEqual(SupportedTextSelection.Single, textBox.SupportedTextSelection);
        }

        [TestMethod]
        public void ShouldGetTextSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIEdit textBox = window.UIEdit("@AutomationId='textBox1'");
            var selection = textBox.GetTextSelection();
            Assert.AreEqual(1, selection.Length);
        }

        [TestMethod]
        public void ShouldGetAllEditControls()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIEdit> editControls = window.UIEdits();
            Assert.AreEqual(6, editControls.Count);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}